#include <QtGui/QApplication>
#include "mainwindow.h"
#include "systemcontroller.h"
#include <QtSql>
#include "storage/sql/sqllite.h"
#include "welcomedialog.h"
#include <QDebug>
#include <QTranslator>


int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    int result;

    //Adding the SQL Database
    QSqlDatabase::addDatabase("QSQLITE", "sqlDb");

    //Creating the SystemController Class
    SystemController *systemController = new SystemController();

    //Setting the Language
    QTranslator translator;
    QString language = systemController->getSettings()->value("settings/language").toString();
    if (language=="")
    {
        language = QLocale::system().name();
        language.truncate(language.lastIndexOf('_'));
    }

    //Try to load the File
    QString translationPath = QApplication::applicationDirPath();
    translationPath.append("/translation");
    if (translator.load("trainermanagement_" + language, translationPath))
    {
        a.installTranslator(&translator);
        systemController->setLanguage(language);
    }
    else
        systemController->setLanguage("en");

    //First show the Select DatabaseDialog
    WelcomeDialog wDialog(systemController);
    result = wDialog.exec();
    wDialog.close();
    MainWindow *w;

    //Show the Main Window
    if (result == QDialog::Accepted)
    {
        w = new MainWindow(systemController);
        w->show();
    }
    else
    {
        return 0;
    }

    int returnValue = a.exec();
    delete w;
    return returnValue;
}
